home *** CD-ROM | disk | FTP | other *** search
- /*
- Time calibration:
- counts the peaks in the spectrum and divides this number (minus one) by
- the number of channels between the first and the last peak.
- This value is then optionally multiplied by the time
- (in whatever units you like) between two peaks to give the exact time
- between two channels.
- */
-
- #include <stdio.h>
- #include <spec.h>
- #define TMPFILE "tica.all"
-
- float *spc, *err, *tim;
- int peak_min=10,
- diff_min=5;
-
- help()
- {
- printf("time calibration:\n");
- printf("tcal file [options]\n");
- printf("prints time difference between channels\n");
- printf("options:\n");
- printf(" -t n.m set time difference between two peaks\n");
- printf(" -y n set minimum height for peaks\n");
- printf(" -mean set minimum height for peaks to arithmetic mean of spectrum\n");
- printf(" -diff set minimum difference between peaks\n");
- printf(" -peak print peak positions\n");
- printf(" -quiet don't print anything (output goes to %s)\n",TMPFILE);
- exit(0);
- }
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int first,last,sptr,n,m,i,max,npeaks;
- int maxdiff, mindiff;
- char z[80];
- float x,y,timd=1.0,tica_err;
- FILE *fp;
-
- checkopt(argc,argv,"-dummy",z);
- spc = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
- err = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
- tim = (float *) calloc(_MAXSPCLEN+2,sizeof(float));
-
- first=0; last=0; npeaks=0;
- if(checkopt(argc,argv,"-t",z)) timd=atosf(z);
- if(checkopt(argc,argv,"-y",z)) peak_min=atoi(z);
- if(checkopt(argc,argv,"-diff",z)) diff_min=atoi(z);
- max=readspec(argv[1],spc,err,tim,z);
- if(checkopt(argc,argv,"-mean",z)) {
- for(n=0;n<max;n++) peak_min = peak_min + spc[n];
- peak_min = peak_min / max;
- peak_min = 2 * peak_min;
- }
- /* now searching for peaks */
- sptr=0; maxdiff = 0; mindiff = 100000;
- while(sptr<max) {
- sptr=sptr+1;
- while(spc[sptr] <= spc[sptr+1]) { /* find next maximum */
- sptr=sptr+1;
- if(sptr>max) break;
- }
- n=spc[sptr];
- if(n<peak_min) continue;
- i=sptr-last;
- if(i<diff_min) continue;
- last=sptr;
- if(first==0) first=sptr;
- if(npeaks > 0) {
- if(i > maxdiff) maxdiff = i;
- if(i < mindiff) mindiff = i;
- }
- npeaks=npeaks+1;
- if(checkopt(argc,argv,"-peak",z)) {
- printf("peak at %d difference %d height %d\n",sptr,i,n);
- }
- while(spc[sptr] > spc[sptr+1]) { /* find next minimum */
- sptr=sptr+1;
- if(sptr>max) break;
- }
- }
- y = ((float)(npeaks - 1)) / ((float)(last - first));
- y = y * timd;
- tica_err = (((float)(maxdiff - mindiff))*1.4142)/((float)(last - first));
- tica_err = y * tica_err;
- if(!checkopt(argc,argv,"-quiet",z)) {
- printf("%d peaks found\n",npeaks);
- printf("time between channels is %5.5f +- %5.5f\n",y,tica_err);
- }
- fp=fopen(TMPFILE,"w");
- fprintf(fp,"%E\n",y);
- fclose(fp);
- return(0);
- }
-
-